"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 拷贝数据
Case Name   : copy from模式下指定smalldatetime_format参数
Create At   : 2022/7/11
Owner       : opentestcase002
Description :
    1.创建测试表
    drop table if exists t_copy_106;
    create table t_copy_106(sk integer,id varchar(16),name varchar(20),
    create_smalldatetime smalldatetime);
    2.构造数据文件并写入内容
    3.copy from模式下指定smalldatetime_format不合法的smalldatetime格式
    copy t_copy_106 from '/xx/testcopy106.dat' with(format 'text',
    smalldatetime_format 'YYYY-M-D HH:MI:SS');
    4.copy from模式,binary格式下指定smalldatetime_format
    copy t_copy_106 from '/xx/testcopy106.dat' with(format 'binary',
    smalldatetime_format 'YYYY-MM-DD HH:MI:SS');
    5.copy from模式下指定smalldatetime_format合法的smalldatetime格式
    copy t_copy_106 from '/xx/testcopy106.dat' with(format 'text',
    smalldatetime_format 'YYYY-MM-DD HH:MI:SS');
    6.清理环境
    drop table t_copy_106;
    rm -rf /xx/testcopy106.dat
Expect      :
    1.创建测试表并插入数据成功
    2.构造数据文件并写入内容成功
    3.copy失败
    4.copy失败
    5.copy成功
    6.清理环境
History     :
"""
import unittest
import os

from yat.test import Node
from yat.test import macro
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant


class CopyFile(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_user = Node(node='PrimaryDbUser')
        self.Constant = Constant()
        self.common = Common()
        self.tb_name1 = 't_copy_106_1'
        self.tb_name2 = 't_copy_106_2'
        self.file_name = 'testcopy106.dat'
        self.copy_dir_path = os.path.join(macro.DB_INSTANCE_PATH,
                                          'pg_copydir')

    def test_copy_file(self):
        text = '-----step1:创建测试表并对测试表插入数据' \
               'expect:创建测试表并插入数据成功-----'
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name1};" \
            f"drop table if exists {self.tb_name2};"\
            f"create table {self.tb_name2} (sk integer,id varchar(16)," \
            f"name varchar(20),create_timestamp timestamp);" \
            f"create table {self.tb_name1}(sk integer,id varchar(16)," \
            f"name varchar(20),create_smalldatetime smalldatetime);" \
            f"insert into {self.tb_name2} values " \
            f"(001,'sk1','tt1','2021-11-01 01:02:30 UTC');" \
            f"insert into {self.tb_name2} values " \
            f"(002,'sk2','tt2','2022-01-23 02:02:30 UTC');" \
            f"insert into {self.tb_name2} values " \
            f"(003,'sk3','tt3','2000-12-23 03:02:30 UTC');"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, sql_res,
                      '执行失败:' + text)
        self.assertIn(self.Constant.INSERT_SUCCESS_MSG, sql_res,
                      '执行失败:' + text)

        text = '-----step2:构造数据文件;expect:构造数据文件成功-----'
        self.log.info(text)
        excute_cmd = f'''mkdir {self.copy_dir_path};
            touch {os.path.join(self.copy_dir_path, self.file_name)};'''
        self.log.info(excute_cmd)
        msg = self.common.get_sh_result(self.pri_user, excute_cmd)
        self.log.info(msg)
        sql_cmd = f"copy {self.tb_name2} to '" \
            f"{os.path.join(self.copy_dir_path, self.file_name)}';"
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('COPY 3', sql_res, '执行失败:' + text)

        text = '-----step3:copy from模式下指定smalldatetime_format不合法的' \
               'smalldatetime格式;expect:copy失败-----'
        self.log.info(text)
        sql_cmd = f"copy {self.tb_name1} from '" \
            f"{os.path.join(self.copy_dir_path, self.file_name)}' " \
            f"with(format 'text',smalldatetime_format 'YYYY-M-D HH:MI:SS');"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('ERROR', sql_res, '执行失败:' + text)

        text = '-----step4:copy from模式,binary格式下指定smalldatetime_format;' \
               'expect:copy失败-----'
        self.log.info(text)
        sql_cmd = f"copy {self.tb_name1} from '" \
        f"{os.path.join(self.copy_dir_path, self.file_name)}' " \
        f"with(format 'binary',smalldatetime_format 'YYYY-MM-DD HH:MI:SS');"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('ERROR', sql_res, '执行失败:' + text)

        text = '----step5:copy from模式下指定smalldatetime_format合法的' \
               'smalldatetime格式;expect:copy成功----'
        self.log.info(text)
        sql_cmd = f"copy {self.tb_name1} from '" \
        f"{os.path.join(self.copy_dir_path, self.file_name)}' " \
        f"with(format 'text',smalldatetime_format 'YYYY-MM-DD HH:MI:SS');"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        sql_cmd = f"select * from {self.tb_name1};"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('3 rows', sql_res)

    def tearDown(self):
        text = '-----step6:清理环境;expect:清理环境成功-----'
        self.log.info(text)
        sql_cmd = self.pri_sh.execut_db_sql(
            f"drop table {self.tb_name1},{self.tb_name2};")
        self.log.info(sql_cmd)
        excute_cmd = f'''rm -rf {self.copy_dir_path}'''
        self.log.info(excute_cmd)
        msg = self.common.get_sh_result(self.pri_user, excute_cmd)
        self.log.info(msg)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, sql_cmd,
                      '执行失败:' + text)
        self.assertEqual(len(msg), 0, '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
